<template>
    <div class="search">
        <Card>
            <div v-hasRole="'ROLE_SCHOOL'">
                <Row>
                    <Form ref="searchForm" v-model="searchForm" inline :label-width="labelWidth70">
                        <!--            <FormItem label="届别" prop="jiebie">-->
                        <!--              <DatePicker-->
                        <!--                  class="data"-->
                        <!--                  :open="open"-->
                        <!--                  v-model="jb"-->
                        <!--                  format="yyyy"-->
                        <!--                  :clearable=false-->
                        <!--                  type="year"-->
                        <!--                  @on-change="handleChange"-->
                        <!--              >-->
                        <!--                <a href="javascript:void(0)" @click="handleClick">-->
                        <!--                  <Icon type="md-arrow-dropdown" />-->
                        <!--                  <template v-if="searchForm.jiebie === ''">请选择</template>-->
                        <!--                  <template v-else>{{ searchForm.jiebie }}届</template>-->
                        <!--                </a>-->
                        <!--              </DatePicker>-->
                        <!--            </FormItem>-->
                    </Form>
                </Row>
                <Row>
                    <Form class="qyForm" ref="form" :model="form" :rules="formValidate" label-position="top">
                        <FormItem label="填报去向登记开关" prop="qxdjSwitch">
                            <RadioGroup v-model="form.qxdjSwitch">
                                <Radio label="0">关闭</Radio>
                                <Radio label="1">开启</Radio>
                            </RadioGroup>
                        </FormItem>
                        <FormItem label="确认去向登记开关" prop="qrdjSwitch">
                            <RadioGroup v-model="form.qrdjSwitch">
                                <Radio label="0">关闭</Radio>
                                <Radio label="1">开启</Radio>
                            </RadioGroup>
                        </FormItem>
                        <FormItem label="户口信息开关" prop="residenceSwitch">
                            <RadioGroup v-model="form.residenceSwitch">
                                <Radio label="0">关闭</Radio>
                                <Radio label="1">开启</Radio>
                            </RadioGroup>
                        </FormItem>
                        <FormItem label="档案信息开关" prop="archivesSwitch">
                            <RadioGroup v-model="form.archivesSwitch">
                                <Radio label="0">关闭</Radio>
                                <Radio label="1">开启</Radio>
                            </RadioGroup>
                        </FormItem>
                        <FormItem label="去向审核设置" prop="examineSwitch">
                            填报去向登记时，审核方式为：
                            <RadioGroup v-model="form.examineSwitch">
                                <Radio label="0">仅校级审核</Radio>
                                <Radio label="1">先院系再校级审核</Radio>
                            </RadioGroup>
                        </FormItem>
                        <FormItem label="档案审核设置" prop="archivesExamineSwitch" class="mt-8">
                            填报档案登记时，审核方式为：
                            <RadioGroup v-model="form.archivesExamineSwitch">
                                <Radio label="0">仅校级审核</Radio>
                                <Radio label="1">先院系再校级审核</Radio>
                            </RadioGroup>
                        </FormItem>
                        <FormItem label="户口审核设置" prop="accountExamineSwitch" class="mt-8">
                            填报户口登记时，审核方式为：
                            <RadioGroup v-model="form.accountExamineSwitch">
                                <Radio label="0">仅校级审核</Radio>
                                <Radio label="1">先院系再校级审核</Radio>
                            </RadioGroup>
                        </FormItem>
                    </Form>
                </Row>
            </div>
            <div v-hasRole="'ROLE_PROVINCE'">
                <Row>
                    <Form ref="searchForm" v-model="searchForm" inline :label-width="labelWidth70">
                        <!--            <FormItem  label="功能设置" prop="jiebie">-->
                        <!--              <DatePicker-->
                        <!--                  class="data"-->
                        <!--                  :open="open"-->
                        <!--                  v-model="jb"-->
                        <!--                  format="yyyy"-->
                        <!--                  :clearable=false-->
                        <!--                  type="year"-->
                        <!--                  @on-change="handleChange"-->
                        <!--              >-->
                        <!--                <a href="javascript:void(0)" @click="handleClick">-->
                        <!--                  <Icon type="md-arrow-dropdown" />-->
                        <!--                  <template v-if="searchForm.jiebie === ''">请选择</template>-->
                        <!--                  <template v-else>{{ searchForm.jiebie }}届</template>-->
                        <!--                </a>-->
                        <!--              </DatePicker>-->
                        <!--              &lt;!&ndash;              <DatePicker type="year" format="yyyy" v-model="jb" @on-change="getDataList" :clearable="false" placeholder="请选择届别" style="width: 200px"></DatePicker>&ndash;&gt;-->
                        <!--            </FormItem>-->
                    </Form>
                </Row>
                <Row>
                    <Form class="qyForm" ref="form1" :model="form" :rules="formValidate2" label-position="top">
                        <FormItem v-if="isNotProvincialLevel" label="本省填报去向信息开关" prop="qxdjSwitch">
                            <RadioGroup v-model="form.qxdjSwitch">
                                <Radio label="0">关闭</Radio>
                                <Radio label="1">开启</Radio>
                            </RadioGroup>
                        </FormItem>
                        <FormItem  v-if="isNotProvincialLevel" label="本省确认去向登记开关" prop="qrdjSwitch">
                            <RadioGroup v-model="form.qrdjSwitch">
                                <Radio label="0">关闭</Radio>
                                <Radio label="1">开启</Radio>
                            </RadioGroup>
                        </FormItem>
                        <FormItem  v-if="isNotProvincialLevel" label="本省户口信息开关" prop="residenceSwitch">
                            <RadioGroup v-model="form.residenceSwitch">
                                <Radio label="0">关闭</Radio>
                                <Radio label="1">开启</Radio>
                            </RadioGroup>
                        </FormItem>
                        <FormItem v-if="isNotProvincialLevel"  label="本省档案信息开关" prop="archivesSwitch">
                            <RadioGroup v-model="form.archivesSwitch">
                                <Radio label="0">关闭</Radio>
                                <Radio label="1">开启</Radio>
                            </RadioGroup>
                        </FormItem>
                      <FormItem label="本省数据上报开关" prop="dataSwitch">
                        <RadioGroup v-model="form.dataSwitch">
                          <Radio label="0">关闭</Radio>
                          <Radio label="1">开启</Radio>
                        </RadioGroup>
                      </FormItem>
                      <FormItem label="去向数据全量上报开关" prop="jyqxDataSwitch">
                          <RadioGroup v-model="form.jyqxDataSwitch">
                              <Radio label="0">关闭</Radio>
                              <Radio label="1">开启</Radio>
                          </RadioGroup>
                      </FormItem>
                        <FormItem label="困难群体标记编辑开关" prop="knqtbjSwitch">
                            <RadioGroup v-model="form.knqtbjSwitch">
                                <Radio label="0">关闭</Radio>
                                <Radio label="1">开启</Radio>
                            </RadioGroup>
                        </FormItem>
                    </Form>
                    <Divider />
                    <Form class="qyForm" :model="fixedForm" label-position="top">
                        <FormItem label="学校登录短信验证开关" prop="jyqxDataSwitch">
                            <RadioGroup v-model="fixedForm.loginValidateSwitch">
                                <Radio label="0">关闭</Radio>
                                <Radio label="1">开启</Radio>
                            </RadioGroup>
                        </FormItem>
                    </Form>
                </Row>
            </div>

            <Row>
                <div class="cml60">
                    <!--        <Button type="text" @click="modalVisible=false">取消</Button>-->
                    <Button type="primary" :loading="submitLoading" @click="handleSubmit">提交</Button>
                </div>

                <!--          <Button type="primary" :loading="submitLoading" @click="handleSubmit">提交</Button>-->
            </Row>

            <!--      <Row>-->
            <!--        <Table :loading="loading" border :columns="columns" :data="data" ref="table" sortable="custom" @on-sort-change="changeSort" @on-selection-change="changeSelect"></Table>-->
            <!--      </Row>-->
        </Card>
    </div>
</template>

<script>
// 根据你的实际请求api.js位置路径修改
import {
    getQyManageList,
    addQyManage,
    editQyManage,
    deleteQyManage,
    getGnSetManage,
    getFixedSelect, editFixedSelect
} from '@/api/qyManage.js';
import { shortcuts } from '@/libs/shortcuts';
import Cookies from 'js-cookie';

// 9月1日-12月31日期间的默认届别是下一年，1月1日-8月31日期间的默认是当前年份
const year = new Date().getFullYear();
const month = new Date().getMonth();
const jiebie = month >= 8 ? year + 1 : year;

export default {
    name: 'qyManage',
    components: {},
    data() {
        return {
            labelWidth70: 70,
            isNotProvincialLevel:true,
            jb: new Date(new Date().getFullYear() + 1, new Date().getMonth(), new Date().getDate()),
            openSearch: true, // 显示搜索
            openTip: true, // 显示提示
            loading: true, // 表单加载状态
            modalType: 0, // 添加或编辑标识
            modalVisible: true, // 添加或编辑显示
            modalTitle: '', // 添加或编辑标题
            searchForm: {
                // 搜索框初始化对象
                pageNumber: 1, // 当前页数
                pageSize: 10, // 页面大小
                sort: 'createTime', // 默认排序字段
                order: 'desc', // 默认排序方式
                jiebie: jiebie
            },
            form: {
                // 添加或编辑表单对象初始化数据
                jiebie: '',
                qxdjSwitch: '0',
                qrdjSwitch: '0',
                residenceSwitch: '0',
                archivesSwitch: '0',
                examineSwitch: '0',
                archivesExamineSwitch: '0',
                accountExamineSwitch: '0',
                dataSwitch:'0',
                jyqxDataSwitch:'0',
                knqtbjSwitch:'0'
            },
            fixedForm: {
                loginValidateSwitch: false,
            },
            // 表单验证规则
            formValidate: {
                qxdjSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
                qrdjSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
                residenceSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
                archivesSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
                dataSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
                jyqxDataSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
                knqtbjSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
                examineSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
                archivesExamineSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
                accountExamineSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
            },
            formValidate2: {
                qxdjSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
                qrdjSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
                residenceSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
                archivesSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
                dataSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
                jyqxDataSwitch: [{ required: true, message: '不能为空', trigger: 'change' }],
                knqtbjSwitch: [{ required: true, message: "不能为空", trigger: "change" }],
            },
            submitLoading: false, // 添加或编辑提交状态
            selectList: [], // 多选数据

            data: [], // 表单数据
            total: 0, // 表单数据总数
            open: false
        };
    },
    methods: {
        init() {
            this.getDataList();
        },

        handleChange(date) {
            this.$refs.form.resetFields();
            this.$refs.form1.resetFields();
            this.jb = date;
            this.searchForm.jiebie = new Date(date).getFullYear();
            this.open = false;
            this.getDataList();
        },
        changePage(v) {
            this.searchForm.pageNumber = v;
            this.getDataList();
            this.clearSelectAll();
        },
        changePageSize(v) {
            this.searchForm.pageSize = v;
            this.getDataList();
        },
        handleSearch() {
            this.searchForm.pageNumber = 1;
            this.searchForm.pageSize = 50;
            this.getDataList();
        },
        handleReset() {
            this.$refs.searchForm.resetFields();
            this.searchForm.pageNumber = 1;
            this.searchForm.pageSize = 50;
            // 重新加载数据
            this.getDataList();
        },
        changeSort(e) {
            this.searchForm.sort = e.key;
            this.searchForm.order = e.order;
            if (e.order === 'normal') {
                this.searchForm.order = '';
            }
            this.getDataList();
        },
        clearSelectAll() {
            this.$refs.table.selectAll(false);
        },
        changeSelect(e) {
            this.selectList = e;
        },
        getDataList() {
            let dateValue = null;
            if (new Date().getMonth() + 1 > 9) {
                dateValue = new Date().getFullYear() + 1;
            } else {
                dateValue = new Date().getFullYear();
            }
            this.loading = true;
            let distinction = this.getStore('distinction');
            this.searchForm.jiebie = distinction ? distinction : dateValue;
            getGnSetManage(this.searchForm).then(res => {
                console.log('去向管理', res);
                this.loading = false;
                if (res.success) {
                    if (res.result) {
                        res.result.qxdjSwitch = res.result.qxdjSwitch == 1 ? '1' : '0';
                        res.result.qrdjSwitch = res.result.qrdjSwitch == 1 ? '1' : '0';
                        res.result.residenceSwitch = res.result.residenceSwitch == 1 ? '1' : '0';
                        res.result.archivesSwitch = res.result.archivesSwitch == 1 ? '1' : '0';
                        res.result.examineSwitch = res.result.examineSwitch  == 1 ? '1' : '0';
                        res.result.archivesExamineSwitch = res.result.archivesExamineSwitch == 1 ? '1' : '0';
                        res.result.accountExamineSwitch = res.result.accountExamineSwitch == 1 ? '1' : '0';
                        res.result.dataSwitch = res.result.dataSwitch  == 1 ? '1' : '0';
                        res.result.jyqxDataSwitch = res.result.jyqxDataSwitch  == 1 ? '1' : '0';
                        res.result.knqtbjSwitch = res.result.knqtbjSwitch  == 1 ? '1' : '0';
                        this.form = res.result;
                    }
                }
            });
            getFixedSelect().then(res => {
                if (res.success) {
                    console.log(res.result,'获取短信登陆开关设置');
                    if (res.result) {
                      res.result.loginValidateSwitch = res.result.loginValidateSwitch ? '1' : '0';
                      this.fixedForm = res.result;
                    } else {
                      this.fixedForm = {
                        loginValidateSwitch: '0'
                      }
                    }
                }
            })
        },

        handleSubmit() {
            this.$refs.form.validate(valid => {
                this.form.jiebie = new Date(this.jb).getFullYear();
                if (valid) {
                    this.submitLoading = true;
                    this.form.jiebie = this.getStore('distinction') || '2025';
                    let param = this.form
                    delete param.updateTime
                    delete param.createTime
                    editFixedSelect(this.fixedForm).then(res => {
                        if (res.success) {
                            editQyManage(param).then(res => {
                                this.submitLoading = false;
                                if (res.success) {
                                    this.$message.success('操作成功');
                                    this.getDataList();
                                    this.modalVisible = false;
                                }
                            });
                        }
                    });
                    this.submitLoading = false;
                }
            });
        },
        add() {
            this.modalType = 0;
            this.modalTitle = '添加';
            this.$refs.form.resetFields();
            delete this.form.id;
            this.modalVisible = true;
        },
        edit(v) {
            this.modalType = 1;
            this.modalTitle = '编辑';
            this.$refs.form.resetFields();
            // 转换null为""
            for (let attr in v) {
                if (v[attr] === null) {
                    v[attr] = '';
                }
            }
            let str = JSON.stringify(v);
            let data = JSON.parse(str);
            this.form = data;
            this.modalVisible = true;
        },
        remove(v) {
            this.$Modal.confirm({
                title: '确认删除',
                // 记得确认修改此处
                content: '您确认要删除该条数据?',
                loading: true,
                onOk: () => {
                    // 删除
                    deleteQyManage({ ids: v.id }).then(res => {
                        this.$Modal.remove();
                        if (res.success) {
                            this.$Message.success('操作成功');
                            this.clearSelectAll();
                            this.getDataList();
                        }
                    });
                }
            });
        },
        delAll() {
            if (this.selectList.length <= 0) {
                this.$Message.warning('您还未选择要删除的数据');
                return;
            }
            this.$Modal.confirm({
                title: '确认删除',
                content: '您确认要删除所选的 ' + this.selectList.length + ' 条数据?',
                loading: true,
                onOk: () => {
                    let ids = '';
                    this.selectList.forEach(function (e) {
                        ids += e.id + ',';
                    });
                    ids = ids.substring(0, ids.length - 1);
                    // 批量删除
                    deleteQyManage({ ids: ids }).then(res => {
                        this.$Modal.remove();
                        if (res.success) {
                            this.$Message.success('操作成功');
                            this.clearSelectAll();
                            this.getDataList();
                        }
                    });
                }
            });
        },
        onRestWindow() {
            this.labelWidth110 = this.searchLabelWidth(this.labelWidth110)
        }
    },
    mounted() {
        this.onRestWindow()
        window.addEventListener('resize',this.onRestWindow)
        let userInfo = JSON.parse(Cookies.get('userInfo')) ;
        if(userInfo.resource == 1){
            this.isNotProvincialLevel = false
        }
        this.init();
    },
    beforeDestroy() {
        window.removeEventListener('resize',this.onRestWindow)
    }
};
</script>
<style lang="less" scoped>
// 建议引入通用样式 具体路径自行修改 可删除下面样式代码
// @import "../../../styles/table-common.less";
.search {
    .operation {
        margin-bottom: 2vh;
    }
    .select-count {
        font-weight: 600;
        color: #40a9ff;
    }
    .select-clear {
        margin-left: 10px;
    }
    .page {
        margin-top: 2vh;
    }
    .drop-down {
        margin-left: 5px;
    }
}
.qyForm {
    margin: 10px 0 0 85px;
    .ivu-form-item-label {
        float: none;
        display: inline-block;
        padding: 0 0 0.052083rem 0;
        font-weight: 600;
    }
}
</style>
<style lang="less" scoped>
.cml60 {
    margin-left: 60px
}
</style>